<?php
namespace app\index\controller;

use app\index\model\Cate;
use app\index\model\Collect;
use app\index\model\Question;
use app\index\model\Reply;
use app\index\model\Zan;
use think\Db;
use think\Loader;

class Jie extends Common
{

	public function index()
	{

        $uid = session('user') ? session('user')->user_id:'null';

		$qid = input('qid/d');


        if(!cache('jie'.$qid)){

            $view_num = Db::name('question')->where('question_id', $qid)->value('view_num');
            cache('jie'.$qid,$view_num+1,3600);

        }else{
            cache('jie'.$qid,cache('jie'.$qid)+1,3600);
        }
        
		$question = Db::name('question')
		->field('t1.*,t2.cate_name,t3.nickname,t3.face,t4.collect_id')
		->alias('t1')
		->join('cate t2','t1.cid = t2.cate_id')
        ->join('user t3','t1.user_id = t3.user_id')
		->join('collect t4','t4.question_id = t1.question_id and t4.user_id='.$uid,'LEFT')
		->where('t1.question_id',$qid)
		->find();



        // $is_collect = Collect::get(['user_id'=>session('user')->user_id,'question_id'=>$qid]);

        // var_dump($is_collect);


        $list =  Question::where('create_time','>',strtotime('-7 days'))->order('reply_num desc')->limit(10)->select();


        $reply = Db::name('reply')
        ->field('t1.*,t2.nickname,t2.face,t3.zan_id')
        ->alias('t1')
        ->join('user t2','t1.user_id = t2.user_id')
        ->join('zan t3','t3.reply_id = t1.reply_id and t3.user_id='.$uid,'LEFT')
        ->where('question_id',$qid)
        ->order('create_time desc')
        ->select();

		
        $this->assign('question',$question);
        $this->assign('reply',$reply);
		$this->assign('list',$list);
        $this->assign('title',$question['title']);
		return $this->fetch();
	}
    public function add()
    {

    	// var_dump(APP_PATH  );

    	// $data = Db::name('cate')->select();

    	// var_dump($data);

    	$cates = Cate::all();

    	$this->assign('cates',$cates);

        return $this->fetch();
    }

    public function reply()
    {
        $data = input('post.');

        $validate = Loader::validate('reply');

        if(!$validate->check($data)){

            $this->error($validate->getError());
        }

        $data['user_id'] = session('user')->user_id;


        $res = Reply::create($data);

        Db::name('question')->where('question_id',$data['question_id'])->setInc('reply_num');

        if($res){
            $this->success("回复成功");
        }else{
            $this->error('回复失败，请稍后再试');
        }
    }

    public function upload()
    {
    	$file = request()->file('file');
    
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){

                $face = '/uploads/'.date("Ymd")."/".$info->getFilename();

                $this->error('上传失败，请稍后再试',null,['src'=>$face]);

            }else{
                $this->error($file->getError());
                // 上传失败获取错误信息
            }
        }
    }

    public function savedata()
    {
    	$data = input('post.');

    	$validate = Loader::validate('Question');

		if(!$validate->check($data)){

			$this->error($validate->getError());
		}

		$data['user_id'] = session('user')->user_id;


		$res = Question::create($data);

		Db::name('user')->where('user_id', session('user')->user_id)->setDec('kiss', $data['kiss']);

		if($res){
			$this->success("发布成功",url('index/jie/index',['qid'=>$res->question_id]));
		}else{
			$this->error('发布失败，请稍后再试');
		}
    }

    public function collect()
    {
        $qid = input('qid');

        if(!$qid)
            $this->error('非法请求');


        $collect = Collect::get(['user_id'=>session('user')->user_id,'question_id'=>$qid]);

        if($collect){

            $res = Collect::destroy(['user_id'=>session('user')->user_id,'question_id'=>$qid]);

        }else{

            $res = Collect::create(['user_id'=>session('user')->user_id,'question_id'=>$qid]);
        }

        if($res){
            $this->success("操作成功");
        }else{
            $this->error('操作失败，请稍后再试');
        }

    }

    public function zan()
    {
        $rid = input('rid');

        if(!$rid)
            $this->error('非法请求');


        $reply = Zan::get(['user_id'=>session('user')->user_id,'reply_id'=>$rid]);

        if($reply){

           $this->error('你已经点赞过了不能重复点赞');

        }else{

            $res = Zan::create(['user_id'=>session('user')->user_id,'reply_id'=>$rid]);

            Db::name('reply')->where('reply_id', $rid)->setInc('zan_num');

            if($res){
                $this->success("操作成功");
            }else{
                $this->error('操作失败，请稍后再试');
            }
        }
    }
}
